package com.wnhz.work.jy.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.ProductAdapter2;
import com.wnhz.work.jy.fragment.AllOrderFragment;
import com.wnhz.work.jy.fragment.AlreadyDeliverFragment;
import com.wnhz.work.jy.fragment.OrderFragment;
import com.wnhz.work.jy.fragment.WaitDeliverFragment;
import com.wnhz.work.jy.fragment.WaitEvaluateFragment;
import com.wnhz.work.jy.fragment.WaitPayFragment;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ----我的订单
 */
public class MyOrderActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private static final String TAG = "MyOrderActivity";
    private TextView title;
    private TabLayout tabLayout;
    private ViewPager vp_order;
    private FragmentPagerAdapter mAdapter;

    private List<OrderFragment> list_fragment; //定义要装fragment的列表
    private List<String> list_title; //tab名称列表

    private AllOrderFragment fragment1;
    private WaitPayFragment fragment2;
    private WaitDeliverFragment fragment3;
    private AlreadyDeliverFragment fragment4;
    private WaitEvaluateFragment fragment5;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);

        index = getIntent().getIntExtra("index",0);
        initView();
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("订单");

        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        vp_order = (ViewPager) findViewById(R.id.vp_order);

        findViewById(R.id.btn_main_left).setOnClickListener(this);

        //初始化各fragment
        fragment1 = new AllOrderFragment();
        fragment2 = new WaitPayFragment();
        fragment3 = new WaitDeliverFragment();
        fragment4 = new AlreadyDeliverFragment();
        fragment5 = new WaitEvaluateFragment();

        //将fragment装进列表中
        list_fragment = new ArrayList<>();
        list_fragment.add(fragment1);
        list_fragment.add(fragment2);
        list_fragment.add(fragment3);
        list_fragment.add(fragment4);
        list_fragment.add(fragment5);

        //将名称加载tab名字列表
        list_title = new ArrayList<>();
        list_title.add("全部");
        list_title.add("待付款");
        list_title.add("待发货");
        list_title.add("待签收");
        list_title.add("待评价");

        //设置TabLayout的模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);

        //为TabLayout添加tab名称
        for (int i = 0; i < list_title.size(); i++) {
            tabLayout.addTab(tabLayout.newTab().setText(list_title.get(i)));
        }

        mAdapter = new ProductAdapter2(MyOrderActivity.this.getSupportFragmentManager(), list_fragment, list_title);

        //viewpager加载adapter
        vp_order.setAdapter(mAdapter);
        vp_order.setOffscreenPageLimit(1);
        //TabLayout加载viewpager
        tabLayout.setupWithViewPager(vp_order);
        vp_order.setCurrentItem(index,false);
        vp_order.setOnPageChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            default:
                break;
        }
    }



    public void cancalOrder(final String orderId,final int i,final String type){
        if ("2".equals(type)){
            showDialogv7("确认签收", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    orderOperation(orderId, i, type);
                }
            });
        }else {
            orderOperation(orderId, i, type);
        }

    }

    private void orderOperation(String orderId, final int i, String type) {
        Map<String,Object> map = new HashMap<>();
        map.put("orderid",orderId);
        map.put("type",type);
        showDialog();
        XUtil.Post(ConfigUtil.CHANGE,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.d(TAG, "onSuccess() returned: " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("result");
                    if ("1".equals(re)){
                        list_fragment.get(i).getData();
                    }
                    String info = jsonObject.optString("info");
                    Toast.makeText(getApplication(),info,Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        list_fragment.get(position).getData();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
